/*
 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "connector_thread.h"
#include "connector_common.h"

/* Initialization */
bool initConnector(void);
bool initSocket(void);
/* Connector configure struct */
ConnectorConfigure g_connectorConfig = {"connector_config.ini",
                                        LOG_INIT_NAME,
                                        {'\0'},
                                        {'\0'},
                                        0, 0};

/* Thread ID, these are created for extending demand */
DWORD g_DPToKABSThreadID;
fsl::LogStream g_default_log;
fsl::LogStream g_board_send_log, g_board_recv_log;

int main(void)
{
    bool result1 = initSocket();
    bool result2 = initConnector();

    if (result1 && result2)
    {
        /* Create thread for DP --> KABS */
        sendOrderToKABS();
    }
    else
    {
        g_default_log<< tstamp <<"Initial Failed, please set config file correctly and restart.\n";
    }
    return 0;
}



/*FUNCTION*********************************************************************
 *
 * Function Name : initConnector
 * Description   : Configure Connector according to .ini file. The .ini file's
 * name is defined in global struct variable g_connectorConfig. This function
 * read ports information used for create listening sockets and heartbeat
 * period as well. As port 0 is NOT supposed to be used in this application,
 * reading any 0 port will cause this initialization fails.
 *END*************************************************************************/
bool initConnector(void)
{
    char full_path[256];

    /* Set configure information */
    sprintf(full_path, ".\\%s", g_connectorConfig.configFileName);
    ::GetPrivateProfileStringA("TCPIP", "server_ip",
                               "---.---.---.---", \
                                g_connectorConfig.serverIP,
                                BUFFER_LEN+1, full_path);
    g_connectorConfig.portRequest = ::GetPrivateProfileIntA("TCPIP", "port_request", 0, full_path);
    g_connectorConfig.portComm = ::GetPrivateProfileIntA("TCPIP", "port_comm", 0, full_path);

    if (g_connectorConfig.portRequest == 0 || g_connectorConfig.portComm == 0 ||
        strcmp(g_connectorConfig.serverIP, "---.---.---.---") == 0)
        return false;

    g_default_log << tstamp << "Server IP: "<<g_connectorConfig.serverIP<<"\n"<<\
                     tstamp << "Request Port: "<<g_connectorConfig.portRequest<<"\n"<<\
                     tstamp << "Comm Port: "<<g_connectorConfig.portComm<<"\n"<<\
                     tstamp <<"Waiting for targetBoard info.\n";

    /* Wait for Dapeng's calling */
    gets(g_connectorConfig.targetBoard);

    g_default_log << tstamp << "From Dapeng: "<< g_connectorConfig.targetBoard;

    /* Modify log file name according to targetBoard */
    char log_file_name[BUFFER_LEN];

    /* Create send log file name */
    strcpy(log_file_name, g_connectorConfig.targetBoard);
    strcat(log_file_name, "_send");
    strcpy(g_connectorConfig.sendLogFileName, log_file_name);
    /* Create recv log file name */
    strcpy(log_file_name, g_connectorConfig.targetBoard);
    strcat(log_file_name, "_recv");
    strcpy(g_connectorConfig.recvLogFileName, log_file_name);

    g_board_send_log = *(new fsl::LogStream(g_connectorConfig.sendLogFileName));
    g_board_recv_log = *(new fsl::LogStream(g_connectorConfig.recvLogFileName));

    FILE *fp = fopen(g_board_recv_log.log_file, "w");
    if (fp != NULL)
    {
        fprintf(fp, "");
        fclose(fp);
    }

    return true;
}

/*FUNCTION*********************************************************************
 *
 * Function Name : initSocket
 * Description   : Initiates use of the wsock32.dll.
 *END*************************************************************************/
bool initSocket(void)
{
    WSADATA wsa_data;

    uint32_t i_result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
    if (i_result != NO_ERROR)
    {
        dispTime();
        printf("WSAStartup failed: %d\n", i_result);

        g_default_log<< tstamp <<"WSAStartup failed: " << i_result <<"\n";

        return false;
    }
    return true;
}

/*EOF*/
